// pages/messages/messages.ts
import { MessageData, MessageType } from '../../components/message-card/message-card.ts';
import { Page } from '../../miniprogram_npm/@types/wx/index';

// 模拟消息数据
const MOCK_MESSAGES: MessageData[] = [
  {
    id: 'msg1',
    type: MessageType.System,
    title: '系统通知',
    content: '您的账号已成功升级为会员，享受更多专属权益',
    sendTime: new Date(Date.now() - 1000 * 60 * 5).toISOString(),
    isRead: false
  },
  {
    id: 'msg2',
    type: MessageType.Comment,
    sender: {
      name: '张小明',
      avatar: '/images/avatars/zhangxm.jpg',
      isVerified: true
    },
    content: '您发布的文章《小程序开发指南》写得非常好，很有帮助！',
    sendTime: new Date(Date.now() - 1000 * 60 * 30).toISOString(),
    isRead: false,
    actionable: true,
    actionText: '回复'
  },
  {
    id: 'msg3',
    type: MessageType.Like,
    sender: {
      name: '李华',
      avatar: '/images/avatars/lihua.jpg'
    },
    content: '赞了您的评论',
    sendTime: new Date(Date.now() - 1000 * 60 * 60 * 2).toISOString(),
    isRead: true
  },
  {
    id: 'msg4',
    type: MessageType.Notification,
    title: '活动提醒',
    content: '明日将举办小程序开发线上沙龙，点击查看详情',
    imageUrl: '/images/events/salon.jpg',
    sendTime: new Date(Date.now() - 1000 * 60 * 60 * 24).toISOString(),
    isRead: true,
    actionable: true,
    actionText: '查看'
  }
];

Page({
  data: {
    messages: [] as MessageData[],
    unreadCount: 0,
    isLoading: true
  },

  onLoad() {
    // 模拟网络请求
    setTimeout(() => {
      this.setData({
        messages: MOCK_MESSAGES,
        unreadCount: MOCK_MESSAGES.filter(msg => !msg.isRead).length,
        isLoading: false
      });
    }, 800);
  },

  // 处理消息卡片点击
  handleCardTap(e: { detail: { messageId: string } }) {
    const { messageId } = e.detail;
    console.log('消息卡片点击:', messageId);
    
    // 标记消息为已读
    this.markAsRead(messageId);
    
    // 根据消息类型跳转到不同页面
    const message = this.data.messages.find(msg => msg.id === messageId);
    if (message) {
      this.navigateToMessageDetail(message);
    }
  },

  // 处理消息操作按钮点击
  handleActionTap(e: { detail: { messageId: string } }) {
    const { messageId } = e.detail;
    console.log('消息操作点击:', messageId);
    
    // 阻止事件冒泡
    e.stopPropagation();
    
    // 处理具体操作
    const message = this.data.messages.find(msg => msg.id === messageId);
    if (message) {
      this.handleMessageAction(message);
    }
  },

  // 标记消息为已读
  markAsRead(messageId: string) {
    const updatedMessages = this.data.messages.map(msg => {
      if (msg.id === messageId && !msg.isRead) {
        return { ...msg, isRead: true };
      }
      return msg;
    });
    
    this.setData({
      messages: updatedMessages,
      unreadCount: updatedMessages.filter(msg => !msg.isRead).length
    });
    
    // 这里可以添加实际的已读状态同步到服务器的逻辑
  },

  // 跳转到消息详情页
  navigateToMessageDetail(message: MessageData) {
    wx.navigateTo({
      url: `/pages/message-detail/message-detail?id=${message.id}`
    });
  },

  // 处理消息操作
  handleMessageAction(message: MessageData) {
    switch (message.actionText) {
      case '回复':
        wx.navigateTo({
          url: `/pages/reply/reply?messageId=${message.id}`
        });
        break;
      case '查看':
        this.navigateToMessageDetail(message);
        break;
      default:
        console.log('未知操作:', message.actionText);
    }
  },

  // 标记全部已读
  markAllAsRead() {
    const updatedMessages = this.data.messages.map(msg => ({
      ...msg,
      isRead: true
    }));
    
    this.setData({
      messages: updatedMessages,
      unreadCount: 0
    });
  }
});
